package com.cheer.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.IntegralConsumeDao;
import com.cheer.enums.ExchangeTypeEnum;
import com.cheer.enums.LotteryActTypeEnum;
import com.cheer.pojo.IntegralConsume;
import com.cheer.service.IntegralConsumeService;
import com.cheer.vo.CreditDateVO;
import com.cheer.vo.CreditRecordVO;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class IntegralConsumeServiceImpl extends ServiceImpl<IntegralConsumeDao,
        IntegralConsume> implements IntegralConsumeService {


    @Override
    public Integer getExchangeTime(Integer userId) {
        List<IntegralConsume> list = list(new QueryWrapper<IntegralConsume>().lambda()
                .eq(IntegralConsume::getUserId, userId)
                .eq(IntegralConsume::getType, ExchangeTypeEnum.INTEGRAL.getCode())
                .ge(IntegralConsume::getCreateTime, DateUtil.beginOfDay(new Date())));
        return list.size();
    }

    @Override
    public void save(Integer userId, Integer exchange, Integer  exchangeNum,LotteryActTypeEnum lotteryActTypeEnum) {

        IntegralConsume record = new IntegralConsume();
        record.setUserId(userId);
        record.setIntegral(exchange);
        record.setExchangeNum(exchangeNum);
        record.setCreateTime(new Date());
        record.setType(lotteryActTypeEnum.getCode());
        save(record);
    }

    @Override
    public List<CreditDateVO> listByUserId(Integer userId) {

        List<IntegralConsume> list = list(new QueryWrapper<IntegralConsume>().lambda()
                .eq(IntegralConsume::getUserId, userId)
                .eq(IntegralConsume::getType, ExchangeTypeEnum.FLOWER.getCode()).orderByDesc(IntegralConsume::getId).last("limit 10"));
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<CreditRecordVO> creditRecordList = list.stream().map( consume -> {
            CreditRecordVO creditRecordVO = new CreditRecordVO();
            creditRecordVO.setName("积分兑换小红花");
            creditRecordVO.setTime(DateUtil.format(consume.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
            creditRecordVO.setValue( "+"+consume.getExchangeNum()+"朵小红花");
            return creditRecordVO;
        }).collect(Collectors.toList());
        // 进行对时间的map
        Map<String, List<CreditRecordVO>> timeMap = creditRecordList.stream()
                .collect(Collectors.groupingBy(creditRecordVO ->
                        DateUtil.format( DateUtil.parse(creditRecordVO.getTime(), DatePattern.NORM_DATETIME_PATTERN)
                                , DatePattern.NORM_DATE_FORMATTER)
        ));

        List<CreditDateVO> result = new ArrayList<>();
        timeMap.forEach( (time, credList) ->{
            CreditDateVO creditDateVO = new CreditDateVO();
            creditDateVO.setDate(time);
            creditDateVO.setCredit(credList);
            result.add(creditDateVO);
        });

        // 截取前五条数据
        return result;
    }
}
